﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;
using System.Linq.Expressions;
using System.Diagnostics.Contracts;
using System.Collections.Concurrent;
using PrairieAsunder.Models;

namespace PrairieAsunder.Common
{
    public static class Extensions
    {
        public static double MinMax(this double value, double min, double max)
        {
            var valMinned = Math.Max(value, min);
            var valMinnedAndMaxed = Math.Min(valMinned, max);
            return valMinnedAndMaxed;
        }

        public static double Range(this Random random, double max)
        {
            return random.NextDouble() * max;
        }

        /// <summary>
        /// Converts a Like database object into a SongLike enum.
        /// This is an extension method; if the Like object is null,
        /// this will return SongLike.None. 
        /// </summary>
        /// <param name="like"></param>
        /// <returns>
        /// If the Like object is null, it returns SongLike.None.
        /// Otherwise, it returns the Like.LikeStatus converted to a SongLike enum.
        /// </returns>
        public static SongLike ToSongLikeEnum(this Like like)
        {
            return Match.Value(like)
                .With(default(Like), SongLike.None)
                .With(l => l != null, l => l.LikeStatus);
        }
    }
}